package com.sgb.kjwl.view.ui.activity.transport.driver.trans;

import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.DialogChooseErweimaBinding;

/**
 * @Author : 黄淋
 * @Time : On 2021/3/9
 * @Description : 选择微信还是支付宝付款
 */
public class ChoosePaymentTypeDialog extends DialogFragment implements View.OnClickListener {
    private DialogClickCallBack dialogClickCallBack;
    private Boolean Flag = false;
    public static final int PAYMENT_WEIXIN = 1;   //微信支付
    public static final int PAYMENT_ZHIFUBAO = 2;  //支付宝支付

    private DialogChooseErweimaBinding binding;
    private boolean isShowFrieght;//是否展示运费
    private String mFrieght;//运费数值

    public ChoosePaymentTypeDialog(boolean isShowFrieght, String frieght) {
        this.isShowFrieght = isShowFrieght;
        this.mFrieght = frieght;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.dialog_choose_erweima, container, false);
        binding.tvWeixin.setSelected(true);
        binding.tvZhifubao.setSelected(false);
        binding.ivStateWeixin.setVisibility(View.VISIBLE);
        binding.ivStateZhifubao.setVisibility(View.INVISIBLE);
        binding.rlWeixin.setOnClickListener(this);
        binding.rlZhifubao.setOnClickListener(this);

        binding.tvTitle.setText(isShowFrieght ? "收款二维码" : "面对面收款");
        binding.llFrieght.setVisibility(isShowFrieght ? View.VISIBLE : View.GONE);
        binding.tvFrieght.setText(mFrieght + "元");

        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    protected void initView() {
        binding.tvCancel.setOnClickListener(view1 -> dismiss());
        binding.tvSure.setOnClickListener(view1 -> {
            dialogClickCallBack.onItemClick(binding.tvWeixin.isSelected() ? PAYMENT_WEIXIN : PAYMENT_ZHIFUBAO);
            dismiss();
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_weixin:
                binding.tvWeixin.setSelected(true);
                binding.tvZhifubao.setSelected(false);
                binding.ivStateWeixin.setVisibility(View.VISIBLE);
                binding.ivStateZhifubao.setVisibility(View.INVISIBLE);
                break;
            case R.id.rl_zhifubao:
                binding.tvWeixin.setSelected(false);
                binding.tvZhifubao.setSelected(true);
                binding.ivStateWeixin.setVisibility(View.INVISIBLE);
                binding.ivStateZhifubao.setVisibility(View.VISIBLE);
                break;
        }
    }

    public interface DialogClickCallBack {
        void onItemClick(int ids);
    }


    public ChoosePaymentTypeDialog setDialogClickCallBack(DialogClickCallBack dialogClickCallBack) {
        this.dialogClickCallBack = dialogClickCallBack;
        return this;
    }


    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        //设置对话框背景为透明
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        window.setBackgroundDrawableResource(R.color.transparent);

    }


    @Override
    public void show(FragmentManager manager, String tag) {
        if (!Flag) {
            Flag = true;
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                if (manager.isDestroyed())
                    return;
            }
            try {
                //在每个add事务前增加一个remove事务，防止连续的add
                manager.beginTransaction().remove(this).commit();
                super.show(manager, tag);
            } catch (Exception e) {
                //同一实例使用不同的tag会异常，这里捕获一下
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Flag = false;
    }
}
